package web

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
)

//g.GET("/ws", jweb.WsPage)
//go WsManager.Start()

// WsManager define a ws server manager

func GetGin(corss bool) *gin.Engine {
	g := gin.Default()
	if corss {
		g.Use(cors.New(cors.Config{
			AllowOriginFunc:  func(origin string) bool { return true },
			AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
			AllowHeaders:     []string{"Origin", "Content-Length", "Content-Type"},
			AllowCredentials: true,
			MaxAge:           12 * time.Hour,
		}))
	}

	return g
}

//WsManager
var WsManager = WsClientManager{}

func StartWS(url string, g *gin.Engine) {
	WsManager = WsClientManager{
		Broadcast:  make(chan []byte),
		Register:   make(chan *WsClient),
		Unregister: make(chan *WsClient),
	}

	g.GET(url, WsPage)
	go WsManager.Start()
}
